package cn.huhaoran.yaoyaolingxian.ui.CustomTreeTableImpl;

import com.intellij.openapi.util.IconLoader;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {

    private final Icon branchIcon;
    private final Icon checkedOutIcon;
    private final Icon selectedIcon;  // 新增的选中状态图标

    public CustomTreeCellRenderer() {
        // 使用 IconLoader 加载图标
        this.branchIcon = IconLoader.getIcon("/vcs/branchNode.svg", CustomTreeCellRenderer.class);
        this.checkedOutIcon = IconLoader.getIcon("/icons/currentBranchLabel.svg", CustomTreeCellRenderer.class);
        this.selectedIcon = IconLoader.getIcon("/actions/commit.svg", CustomTreeCellRenderer.class); // 加载选中的图标
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        // 调用父类的方法来保留默认的行为和样式
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

        if (leaf && value instanceof GitBranchNode) {
            GitBranchNode node = (GitBranchNode) value;
/*            if(node.isLeaf()&&node.getChildCount()==0){
                return this;
            }*/
            if (node.isCheckedOut()) {
                if(node.isSelected()){
                    setIcon(selectedIcon);
                    setBackgroundNonSelectionColor(new Color(144, 238, 144));
                    setTextNonSelectionColor(Color.BLACK);
                }else{
                    setIcon(checkedOutIcon);
                }
                //setIcon(node.isSelected() ? selectedIcon : checkedOutIcon); // 使用选中的图标替换原图标
            } else {
                if(node.isSelected()){
                    setIcon(selectedIcon);
                    setBackgroundNonSelectionColor(new Color(144, 238, 144));
                    setTextNonSelectionColor(Color.BLACK);
                }else{
                    setIcon(branchIcon);
                }
                //setIcon(node.isSelected() ? selectedIcon : branchIcon); // 使用选中的图标替换原图标
            }
        }

        return this;
    }

}
